package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.Product;
import cn.wolfcode.shop.domain.ProductSku;
import cn.wolfcode.shop.domain.ProductSkuProperty;
import cn.wolfcode.shop.domain.UserLogin;
import cn.wolfcode.shop.mapper.ProductMapper;
import cn.wolfcode.shop.mapper.ProductSkuMapper;
import cn.wolfcode.shop.mapper.ProductSkuPropertyMapper;
import cn.wolfcode.shop.query.ProductQueryObject;
import cn.wolfcode.shop.service.IProductService;
import cn.wolfcode.shop.utils.Assert;
import cn.wolfcode.shop.utils.PageResult;
import cn.wolfcode.shop.vo.SaveSkuVO;
import cn.wolfcode.shop.vo.ShopCarVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.MessageFormat;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductSkuPropertyMapper productSkuPropertyMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageResult selectForPage(ProductQueryObject qo) {
        int totalCount = productMapper.queryCount(qo);
        List data = productMapper.queryList(qo);
        return new PageResult(data,totalCount,qo.getCurrentPage(),qo.getPageSize());
    }

    /**
     * 生成sku的主页
     * @param productId
     */
    @Override
    public Product selectByProductId(Long productId) {
       return productMapper.selectByPrimaryKey(productId);
    }

    /**
     * 保存SKU
     * @param saveSkuVO
     */
    @Override
    public void productSkuSave(SaveSkuVO saveSkuVO) {
        List<ProductSku> productSkuList = saveSkuVO.getProductSkuList();
        for (ProductSku productSku : productSkuList){
            productSku.setProductId(saveSkuVO.getProduct().getId());
            productSkuMapper.insert(productSku);

            for (ProductSkuProperty productSkuProperty : productSku.getProductSkuPropertyList()){
                productSkuProperty.setProductSkuId(productSku.getId());
                productSkuPropertyMapper.insert(productSkuProperty);
            }
        }
    }

    /**
     * 添加购物车接口
     * @param productSkuId
     */
    @Override
    public void shopcar(String token,Long productSkuId, Integer number) {
        //1.获取用户对象
        UserLogin user = (UserLogin) redisTemplate.opsForValue().get(token);
        Assert.notNull(user,"没有登陆,请先登录");
        
        //2.大key
        Long maxKey =user.getId();
        
        //3.小key
        String minKey = MessageFormat.format("productSkuId:{0}", productSkuId);
        
        //4.查询缓存,是否存在当前sku的商品,如果有 数量+ number
        ShopCarVo shopCarVo = (ShopCarVo) redisTemplate.boundHashOps(maxKey).get(minKey);


        //如果不为空,表示一样的商品,直接在数量+ number
         if(shopCarVo != null){
             shopCarVo.setNumber(shopCarVo.getNumber()+number);
         }else {
             //sku商品对象
             ProductSku productSku = productSkuMapper.selectByPrimaryKey(productSkuId);
             //商品对象
             Product product = productMapper.selectByPrimaryKey(productSku.getProductId());
             shopCarVo = new ShopCarVo();
             shopCarVo.setTitle(product.getName());
             shopCarVo.setImage(product.getImage());


             List<ProductSkuProperty> productSkuPropertyList = productSku.getProductSkuPropertyList();
             StringBuilder sb = new StringBuilder();
             for (ProductSkuProperty productSkuProperty : productSkuPropertyList) {
                 String skuName = productSkuProperty.getSkuProperty().getName();
                 String skuValue = productSkuProperty.getValue();

                 sb.append(skuName).append(":").append(skuValue).append(":");
             }
             shopCarVo.setSkuString(sb.toString());

             shopCarVo.setPrice(productSku.getPrice());
             shopCarVo.setProductSkuId(productSku.getProductId());
             shopCarVo.setNumber(number);

         }

        //将购物车中信息存入redis 中 ,使用的数据结构是 map
        redisTemplate.boundHashOps(maxKey).put(minKey,shopCarVo);
    }
}
